//
//  HMTabelViewCell.m
//  todayHappyTalk
//
//  Created by qu on 2017/1/3.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "HMTabelViewCell.h"
#import "UIImage+CycleImage.h"
#import "HMTodayHappyTalkModel.h"
#import "UIColor+Addition.h"
#import "UILabel+Addition.h"
#import "Masonry.h"
#import "HMShoppingCarModel.h"
#import "HMShoppingCarBtn.h"
/*
 {
 "resource_id":723,
 "price":4.99,
 "file_size":26145881,
 "image_url":"http://dgtt.besttoptoday.com/reader/201612/Yrzi5PnCkh.jpg",
 "resource_name":"12.27专治各种想不开丨商业最大的奥秘是什么？就这两个字！",
 "reader_name":"杨铄今",
 "resource_type":"4",
 "reader_introduce":"今日排行榜自媒体3.0联盟最胖的发起人中国管理科学学会工商管理专业委员会（双志精英会）创始人，与数千名企业家、明星、学者、媒体主编、社会公知、创业者及各行业翘楚组成了国内最具规模的母基金平台、非工作深度社交生态圈、金融多资产管理平台总之，是一只最帅的创业胖子。",
 "resource_content":"12.27专治各种想不开丨商业最大的奥秘是什么？就这两个字！",
 "add_time":1482909408,
 "resource_enclosure":"1637",
 "reader_header_url":"http://dgtt.besttoptoday.com/bookreader/image/20161214/2016121415140013462.jpg",
 "reader_id":12
 },
 */
@interface HMTabelViewCell ()

@property (nonatomic, weak) UIImageView *imageHeaderView;
@property (nonatomic, weak) UIImageView *imageBigView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *detailLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *priceLabel;
@property (nonatomic, weak) UILabel *dateLabel;

@property (nonatomic, strong) HMShoppingCarBtn *shoppingCarBtn;
@end
@implementation HMTabelViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor colorWithHex:0xf5f5f5];
        [self setUpUI];
        
    }
    return self;
}

- (void)setSelectionStyle:(UITableViewCellSelectionStyle)selectionStyle {
//    [super setSelectionStyle:selectionStyle];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
}

- (void)setHMModel:(HMTodayHappyTalkModel *)HMModel {
    
    _HMModel = HMModel;
    
    self.nameLabel.text = HMModel.reader_name;
    
    self.titleLabel.text = HMModel.resource_name;
    
    self.detailLabel.text = HMModel.resource_content;
    
    
    self.timeLabel.text = [NSString stringWithFormat:@"音频时长:%zd分%zd秒",[HMModel.resource_enclosure integerValue]/60,[HMModel.resource_enclosure integerValue]%60];
    
    self.priceLabel.text = [NSString stringWithFormat:@"%.2lf",HMModel.price];
    
    NSURL *url = [NSURL URLWithString:HMModel.reader_header_url];
    
    [self.imageHeaderView sd_setImageWithURL:url];
    
    NSURL *bigUrl = [NSURL URLWithString:HMModel.image_url];
    
    [self.imageBigView sd_setImageWithURL:bigUrl];
    
    self.shoppingCarBtn.image_url = HMModel.image_url;
    self.shoppingCarBtn.resource_name = HMModel.resource_name;
    self.shoppingCarBtn.price = [NSString stringWithFormat:@"%.2lf",HMModel.price];
    self.shoppingCarBtn.resource_content = HMModel.resource_content;
    self.shoppingCarBtn.resource_enclosure = HMModel.resource_enclosure;
    self.shoppingCarBtn.resource_id = [NSString stringWithFormat:@"%zd",HMModel.resource_id];
    
    self.dateLabel.text = [NSString getAddDayForDate:HMModel.add_time];
    
}

- (void)setUpUI {
    
    UIView *backGroundView = [[UIView alloc]init];
    
    backGroundView.backgroundColor = [UIColor whiteColor];
    
    [self.contentView addSubview:backGroundView];
    
    [backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(10, 10, 0, 10));
    }];
    
    
    UILabel *dateLabel = [[UILabel alloc]init];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.text = @"";
    dateLabel.font = [UIFont systemFontOfSize:17];
    [dateLabel sizeToFit];
    [self.contentView addSubview:dateLabel];
    self.dateLabel = dateLabel;
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.equalTo(backGroundView);
        make.left.equalTo(backGroundView);
        make.height.offset(50);
        make.width.offset(62);
        
    }];
    
    UIView *verticalView = [[UIView alloc]init];
    verticalView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:verticalView];
    
     [verticalView mas_makeConstraints:^(MASConstraintMaker *make) {
        
         make.centerY.equalTo(dateLabel.mas_centerY);
         make.left.equalTo(dateLabel.mas_right);
         make.height.offset(30);
         make.width.offset(1);
     }];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage cycleImageNamed:@"littleImage"]];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.width.offset(35);
        make.centerY.equalTo(verticalView.mas_centerY);
        make.left.equalTo(verticalView.mas_right).offset(12);
    }];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    
    nameLabel.text = @"杨铄今";
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = [UIFont systemFontOfSize:17];
    [self.contentView addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(50);
        make.centerY.equalTo(imageView.mas_centerY);
        make.left.equalTo(imageView.mas_right).offset(12);
    }];
    
    UILabel *talkLabel = [[UILabel alloc]init];
    
    talkLabel.text = @"讲述";
    
    talkLabel.font = [UIFont systemFontOfSize:15];
    
    talkLabel.textAlignment = NSTextAlignmentCenter;
    
    talkLabel.textColor = [UIColor colorWithHex:0x6e6e6e];
    
    [self.contentView addSubview:talkLabel];
    
    [talkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.height.offset(50);
        make.centerY.equalTo(nameLabel.mas_centerY);
        make.left.equalTo(nameLabel.mas_right).offset(12);
        
    }];
    
    UIView *horizontalView = [[UIView alloc]init];
    horizontalView.backgroundColor = [UIColor colorWithHex:0xaaaaaa];
    
    [self.contentView addSubview:horizontalView];
    
    [horizontalView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(dateLabel.mas_bottom).offset(5);
        make.left.offset(15);
        make.height.offset(0.5);
        make.right.equalTo(backGroundView.mas_right);
    }];
    
    UIImage *image = [UIImage imageNamed:@"bigImage"];
    UIImageView *bigImageView = [[UIImageView alloc]initWithImage:image];
    [self.contentView addSubview:bigImageView];
    
    [bigImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(horizontalView.mas_bottom).offset(16);
        make.left.equalTo(backGroundView.mas_left).offset(16);
        make.width.offset(82);
        make.height.offset(111);
    }];
    
    UILabel *titleLabel = [UILabel hm_labelWithText:@"11" fontSize:15 color:[UIColor blackColor]];
    [self.contentView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(horizontalView.mas_bottom).offset(16);
        make.left.equalTo(bigImageView.mas_right).offset(10);
        make.right.equalTo(backGroundView.mas_right).offset(-10);
    }];
    
    self.titleLabel = titleLabel;
    UILabel *detailLabel = [UILabel hm_labelWithText:@"1" fontSize:13 colorWithHex:0x6e6e6e];
    detailLabel.numberOfLines = 0;
    [self.contentView addSubview:detailLabel];
    
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(11);
        make.left.equalTo(titleLabel.mas_left);
        make.right.equalTo(backGroundView.mas_right).offset(-34);
        
    }];
    self.detailLabel = detailLabel;
    
    UILabel *timeLabel = [UILabel hm_labelWithText:@"2" fontSize:14 color:[UIColor blackColor]];
    [self.contentView addSubview:timeLabel];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(detailLabel.mas_bottom).offset(5);
        make.left.equalTo(titleLabel.mas_left);
        
    }];
    self.timeLabel = timeLabel;
    UILabel *priceLabel = [UILabel hm_labelWithText:@"1" fontSize:12 colorWithHex:0xffa52b];
    
    [self.contentView addSubview:priceLabel];
    
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(12);
        make.left.equalTo(titleLabel.mas_left);
    }];
    self.priceLabel = priceLabel;
    
    
    
    UIButton *buyBtn = [[UIButton alloc]init];
    
    [buyBtn setBackgroundImage:[UIImage imageNamed:@"home_btn_buy"] forState:UIControlStateNormal];
    [buyBtn setTitle:@"购买" forState:UIControlStateNormal];
    [buyBtn setTitleColor:[UIColor colorWithHex:0xffd9a6] forState:UIControlStateNormal];
    buyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:buyBtn];
    
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(backGroundView).offset(-8);
        make.right.equalTo(backGroundView).offset(-17);
        make.height.offset(33);
        make.width.offset(58);
    }];
    
    
    HMShoppingCarBtn *carBtn = [[HMShoppingCarBtn alloc]init];

    [self.contentView addSubview:carBtn];
    self.shoppingCarBtn = carBtn;
    [carBtn sizeToFit];
    
    
    [carBtn mas_makeConstraints:^(MASConstraintMaker *make) {
      
        
        make.bottom.equalTo(backGroundView);
        make.right.equalTo(buyBtn.mas_left).offset(-18);
        make.height.width.offset(44);
       
    }];
  
}

- (void)buyBtnClick:(UIButton *)btn {
    
    NSLog(@"购买");
}


@end
